package com.dwzlab.test;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DSTool {
	private static DSTool dsTool = new DSTool();
	
	private DSTool() {
	}
	
	public static DSTool newInstance(){
		return dsTool == null ? new DSTool() : dsTool;
	}
	
	public Connection getConnection(){
		String datasource = "java:comp/env/jdbc/guoli";
		DataSource ds = null;
		Connection conn = null;
		try {
			Context ctx = new InitialContext();
			ds = (DataSource)ctx.lookup(datasource);
			conn = ds.getConnection();
		} catch (NamingException e) {
			System.out.println("lookup datasource failed..."+e.getMessage());
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("get connection failed..."+e.getMessage());
			e.printStackTrace();
		}
		return conn;
	}
	
	public DataSource getDS(){
		String datasource = "java:comp/env/jdbc/guoli";
		DataSource ds = null;
		try {
			Context ctx = new InitialContext();
			ds = (DataSource)ctx.lookup(datasource);
		} catch (NamingException e) {
			System.out.println("lookup datasource failed..."+e.getMessage());
			e.printStackTrace();
		}
		return ds;
	}
}
